using System.IO;
using System.Reflection;
using Smallpointer.Configuration.Common;

namespace Smallpointer.Resource
{
    public class ResourceReader
    {
        private const string DefaultEntityExt = ".config";
        public static Assembly CurrentAssembly = Assembly.GetExecutingAssembly();
        public static string GetEmbeddedEntityMapConfig(string name)
        {
            var files = CurrentAssembly.GetManifestResourceNames();
            var filename = string.Empty;
            foreach (string file in files)
            {
                if (file.ToLower().IndexOf("entities." + name.ToLower()) <= 0) continue;
                filename = file;
                break;
            }
            var stream = CurrentAssembly.GetManifestResourceStream(filename);
            return StreamToString(stream);
        }
        public static string GetDiskEntityMapConfig(string name)
        {
            var file = ConfigurationUtil.GetFullFilename(name + DefaultEntityExt);
            return string.IsNullOrEmpty(file) ? string.Empty : File.ReadAllText(file);
        }
        internal static string StreamToString(Stream stream)
        {
            string content;
            using (var streamReader = new StreamReader(stream))
            {
                content = streamReader.ReadToEnd();
                streamReader.Close();
            }
            return content;
        }
    }
}